import Vue from "vue";
import VueRouter from "vue-router";
import Cookies from "js-cookie";

const Layout = () => import("@/components/layout/Layout");
const Academy = () => import("@/components/report/academy");
const Login = () => import("@/components/login/Login.vue");
const Add = () => import("@/components/admin/add");
const Manage = () => import("@/components/admin/manage");
const TeacherStudent = () => import("@/components/teacherStudent");
const Statistics = () => import("@/components/statistics");
const ExportData = () => import("@/components/exportData");

Vue.use(VueRouter);

const routes = [
    {
        path:'/',
        component:Login
    },
    {
        path: '/admin',
        redirect:'/academy',
        component:Layout,
        // meta 用于面包屑设置
        children:[
            {
                path:'/academy',
                name:'Academy',
                component:Academy,
                meta:[['/academy','打卡查询']]
            },
            {
              path:'/add',
              name:'Add',
              component:Add,
              meta:[['/add','管理设置'],['/add','添加-管理员']]
            },
            {
              path:'/manage',
              name:'Manage',
              component:Manage,
              meta:[['/add','管理设置'],['/manage','管理-管理员']]
            },
            {
              path:'/teacherStudent',
              name:'TeacherStudent',
              component:TeacherStudent,
              meta:[['/teacherStudent','师生管理']]
            },
            {
              path:'/statistics',
              name:'Statistics',
              component:Statistics,
              meta:[['/statistics','图形化统计']]
            },
            {
                path:'/exportData',
                name:'ExportData',
                component:ExportData,
                meta:[['/exportData','数据导出']]
            }
        ]
        
    },

]
const router = new VueRouter({
  routes,
  mode: "history",
});

// 每一次进入的时候判断cookie有没有token
router.beforeEach((to, from, next) => {
  //使用js-cookie存取cookie
  let token = Cookies.get("token");

  if (to.path === "/" || to.path === "/login") {
    if (token != null && token != "" && token != undefined) {
      next("/admin");
    }
    next();
  } else {
    if (token != null && token != "" && token != undefined) {
      next();
    } else {
      alert("请先登录");
      next("/");
    }
  }
});

export default router;
